﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
using Microsoft.Research.Kinect.Audio;
using Microsoft.Speech.Recognition;
using System.IO;
using Microsoft.Speech.AudioFormat;
using System.Windows.Forms;

namespace PowerPointVoiceControl
{
    public partial class ThisAddIn
    {
        private KinectAudioSource kinectSource;
        private SpeechRecognitionEngine sre;
        private EnergyCalculatingPassThroughStream stream;
        private const int WavImageWidth = 500;
        private const int WavImageHeight = 100;
        private double[] energyBuffer = new double[WavImageWidth];

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            InitializeSpeechRecognition();

            Start();
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            try
            {
                kinectSource.Stop();
            }
            catch (Exception ex)
            { }
            sre.RecognizeAsyncCancel();
            sre.RecognizeAsyncStop();
            kinectSource.Dispose();
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        private static RecognizerInfo GetKinectRecognizer()
        {
            Func<RecognizerInfo, bool> matchingFunc = r =>
            {
                string value;
                r.AdditionalInfo.TryGetValue("Kinect", out value);
                return "True".Equals(value, StringComparison.InvariantCultureIgnoreCase) && "en-US".Equals(r.Culture.Name, StringComparison.InvariantCultureIgnoreCase);
            };
            return SpeechRecognitionEngine.InstalledRecognizers().Where(matchingFunc).FirstOrDefault();
        }

        private void InitializeSpeechRecognition()
        {
            RecognizerInfo ri = GetKinectRecognizer();
            if (ri == null)
            {
            }

            try
            {
                sre = new SpeechRecognitionEngine(ri.Id);
            }
            catch
            {
            }

            var colors = new Choices();
            colors.Add("forward");
            colors.Add("back");
            colors.Add("next slide");
            colors.Add("previous slide");

            var gb = new GrammarBuilder();
            gb.Culture = ri.Culture;
            gb.Append(colors);

            // Create the actual Grammar instance, and then load it into the speech recognizer.
            var g = new Grammar(gb);

            sre.LoadGrammar(g);
            sre.SpeechRecognized += sre_SpeechRecognized;
            sre.SpeechHypothesized += sre_SpeechHypothesized;
            sre.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(sre_SpeechRecognitionRejected);

        }

        void sre_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
        {
            ReportSpeechStatus("Rejected: " + (e.Result == null ? string.Empty : e.Result.Text + " " + e.Result.Confidence));
        }


        void sre_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
        {
            ReportSpeechStatus("Hypothesized: " + e.Result.Text + " " + e.Result.Confidence);
        }

        void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result.Confidence < 0.8)
                return;

            switch (e.Result.Text.ToUpperInvariant())
            {
                case "FORWARD":
                case "NEXT SLIDE":
                    if(this.Application.SlideShowWindows.Count > 0)
                        this.Application.SlideShowWindows[1].View.Next();
                    break;
                case "BACK":
                case "PREVIOUS SLIDE":
                    if (this.Application.SlideShowWindows.Count > 0)
                        this.Application.SlideShowWindows[1].View.Previous();
                    break;
                default:
                    break;
            }

            ReportSpeechStatus("Recognized: " + e.Result.Text + " " + e.Result.Confidence);
        }

        private void ReportSpeechStatus(string status)
        {
        }

        private void Start()
        {
            try
            {
                kinectSource = new KinectAudioSource();
                kinectSource.SystemMode = SystemMode.OptibeamArrayOnly;
                kinectSource.FeatureMode = true;
                kinectSource.AutomaticGainControl = false;

                kinectSource.MicArrayMode = MicArrayMode.MicArrayAdaptiveBeam;
                var kinectStream = kinectSource.Start();
                stream = new EnergyCalculatingPassThroughStream(kinectStream);
                sre.SetInputToAudioStream(stream, new SpeechAudioFormatInfo(
                                                      EncodingFormat.Pcm, 16000, 16, 1,
                                                      32000, 2, null));
                sre.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch
            {

            }
        }


        #endregion

        class EnergyCalculatingPassThroughStream : Stream
        {
            private readonly Stream baseStream;
            private int index = 0;
            private readonly double[] energy = new double[WavImageWidth];
            private readonly object syncRoot = new object();
            const int samplesPerPixel = 10;
            int sampleCount = 0;
            double avgSample = 0;

            public EnergyCalculatingPassThroughStream(Stream stream)
            {
                baseStream = stream;
            }

            public override bool CanRead
            {
                get { return baseStream.CanRead; }
            }

            public override bool CanSeek
            {
                get { return baseStream.CanSeek; }
            }

            public override bool CanWrite
            {
                get { return baseStream.CanWrite; }
            }

            public override void Flush()
            {
                baseStream.Flush();
            }

            public override long Length
            {
                get { return baseStream.Length; }
            }

            public override long Position
            {
                get { return baseStream.Position; }
                set { baseStream.Position = value; }
            }

            public void GetEnergy(double[] energyBuffer)
            {
                lock (syncRoot)
                {
                    int energyIndex = index;
                    for (int i = 0; i < energy.Length; i++)
                    {
                        energyBuffer[i] = energy[energyIndex];
                        energyIndex++;
                        if (energyIndex >= energy.Length)
                            energyIndex = 0;

                    }
                }
            }

            public override int Read(byte[] buffer, int offset, int count)
            {
                int retVal = baseStream.Read(buffer, offset, count);
                double a = 0.3;
                lock (syncRoot)
                {
                    for (int i = 0; i < retVal; i += 2)
                    {

                        short sample = BitConverter.ToInt16(buffer, i + offset);
                        avgSample += sample * sample;
                        sampleCount++;

                        if (sampleCount == samplesPerPixel)
                        {
                            avgSample /= samplesPerPixel;

                            energy[index] = .2 + (avgSample * 11) / (int.MaxValue / 2); //2^30 = (2^15)^2
                            energy[index] = energy[index] > 10 ? 10 : energy[index];

                            if (index > 0)
                                energy[index] = energy[index] * a + (1 - a) * energy[index - 1];

                            index++;
                            if (index >= energy.Length)
                                index = 0;
                            avgSample = 0;
                            sampleCount = 0;
                        }

                    }
                }

                return retVal;
            }

            public override long Seek(long offset, SeekOrigin origin)
            {
                return baseStream.Seek(offset, origin);
            }

            public override void SetLength(long value)
            {
                baseStream.SetLength(value);
            }

            public override void Write(byte[] buffer, int offset, int count)
            {
                baseStream.Write(buffer, offset, count);
            }
        }

    }
}
